<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit\Ctl;

use Srv\Apps\Proto\DTO\Skit\SkitTheaterAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTheaterEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTheaterListDTO;
use Srv\Apps\Proto\Enum\PlatformEnum;
use Srv\Apps\Proto\Enum\Skit\SkitTheaterStyleTypeEnum;
use Srv\Apps\Proto\VO\Skit\SkitTheaterListVO;
use Srv\Apps\Proto\VO\Skit\SkitTheaterVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Common\CommJson;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class SkitTheaterCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $thcfId
     * @return array
     * getSkitTheaterSrc
     */
    public function getSkitTheaterSrc(int $thcfId):array
    {
        if($thcfId < 1) return [];
        $isFail     = false;
        $info       = $this->DbAuto->getDataOne('SELECT thcf_id, thcf_platform, thcf_name, thcf_title, thcf_sub_title, skio_ids, thcf_style_type, thcf_sort, thcf_status, thcf_create_time, thcf_update_time FROM '.$this->DbAuto->getTableNameAll('theater_conf').' WHERE thcf_id='.$thcfId.' AND thcf_delete=0', $isFail);
        if($isFail || !isset($info['thcf_id'])) return [];
        return $info;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return SkitTheaterListVO|null
     * getSkitTheaterList
     */
    public function getSkitTheaterList(Page $Page, array $whereData): ?SkitTheaterListVO
    {
        $limitString            = $Page->getLimitString();
        $whereString            = ' WHERE thcf_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case SkitTheaterListDTO::_thcfId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thcf_id='. intval($val);
                    break;
                case SkitTheaterListDTO::_name:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thcf_name LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitTheaterListDTO::_title:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thcf_title LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitTheaterListDTO::_subTitle:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thcf_sub_title LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitTheaterListDTO::_styleType:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thcf_style_type='. intval($val);
                    break;
                case SkitTheaterListDTO::_status:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thcf_status='. intval($val);
                    break;
                case SkitTheaterListDTO::_platform:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'thcf_platform='. intval($val);
                    break;
            }
        }
        $isFail                 = false;
        $sql                    = 'SELECT SQL_CALC_FOUND_ROWS thcf_id, thcf_platform, thcf_name, thcf_title, thcf_sub_title, skio_ids, thcf_style_type, thcf_sort, thcf_status, thcf_create_time, thcf_update_time FROM '.$this->DbAuto->getTableNameAll('theater_conf').$whereString.' ORDER BY thcf_id DESC '.$limitString;
        $list                   = $this->DbAuto->getData($sql, $isFail);
        $totalNum               = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $SkitTheaterVOList      = [];
        if(CommArray::isForeach($list))foreach($list as $info){
            $thcfIdItem         = intval($info['thcf_id']);
            if($thcfIdItem < 1) continue;
            $SkitTheaterVO      = $this->getSkitTheaterVO($info);
            if(!($SkitTheaterVO instanceof SkitTheaterVO)) continue;
            $SkitTheaterVOList[]= $SkitTheaterVO;
        }
        $SkitTheaterListVO      = new SkitTheaterListVO();
        $SkitTheaterListVO->setList($SkitTheaterVOList);
        $SkitTheaterListVO->setPage($Page->getPageVO($totalNum));
        return $SkitTheaterListVO;
    }

    /**
     * @param int $thcfId
     * @return SkitTheaterVO|null
     * getSkitTheaterInfo
     */
    public function getSkitTheaterInfo(int $thcfId):?SkitTheaterVO
    {
        $theaterSrc     = $this->getSkitTheaterSrc($thcfId);
        if(!isset($theaterSrc['thcf_id'])) return null;
        return $this->getSkitTheaterVO($theaterSrc);
    }

    /**
     * @param array $info
     * @return SkitTheaterVO|null
     * getSkitTheaterVO
     */
    public function getSkitTheaterVO(array $info):?SkitTheaterVO
    {
        $thcfId             = intval($info['thcf_id']??0);
        if($thcfId < 1) return null;
        $SkitTheaterVO      = new SkitTheaterVO();
        $SkitTheaterVO->setThcfId($thcfId);
        $SkitTheaterVO->setName(trim($info['thcf_name']));
        $SkitTheaterVO->setTitle(trim($info['thcf_title']));
        $SkitTheaterVO->setSubTitle(trim($info['thcf_sub_title']));
        $skioIdList         = CommJson::decodeArray($info['skio_ids']);
        $skitInfoVOListNew  = [];
        $SkitInfoVOList     = SkitInfoCtl::tag()->getSkitInfoBatch($skioIdList);
        foreach ($skioIdList as $skioIdItem){
            $SkitInfoVO     = $SkitInfoVOList[$skioIdItem]??null;
            if($SkitInfoVO !== null) $skitInfoVOListNew[] = $SkitInfoVO;
        }
        $SkitTheaterVO->setSkitInfoList($skitInfoVOListNew);
        $SkitTheaterVO->setStyleType(SkitTheaterStyleTypeEnum::parseValue($info['thcf_style_type']));
        $SkitTheaterVO->setSort(intval($info['thcf_sort']));
        $SkitTheaterVO->setStatus(intval($info['thcf_status']));
        $SkitTheaterVO->setCreateTime(intval($info['thcf_create_time']));
        $SkitTheaterVO->setUpdateTime(intval($info['thcf_update_time']));
        $SkitTheaterVO->setPlatform(PlatformEnum::parseValue($info['thcf_platform']));
        return $SkitTheaterVO;
    }

    /**
     * @param array $setData
     * @return int
     * skitTheaterAdd
     */
    public function skitTheaterAdd(array $setData):int
    {
        if(count($setData) < 1) return 0;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitTheaterAddDTO::_skioIdList:
                    $skitInfoList   = SkitInfoCtl::tag()->getSkitInfoSrcBatch($val);
                    $skioIdList     = array_map('intval', array_column($skitInfoList, 'skio_id'));
                    $skioIdList     = array_values(array_intersect($val, $skioIdList));
                    $skioIdJson     = CommJson::encodeArray($skioIdList);
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ids=\''.$this->DbAuto->getRealEscapeString($skioIdJson).'\'';
                    break;
                case SkitTheaterAddDTO::_name:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_name=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterAddDTO::_title:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_title=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterAddDTO::_subTitle:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_sub_title=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterAddDTO::_styleType:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_style_type='.intval($val);
                    break;
                case SkitTheaterAddDTO::_sort:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_sort='.intval($val);
                    break;
                case SkitTheaterAddDTO::_status:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_status='. intval($val);
                    break;
                case SkitTheaterAddDTO::_platform:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_platform='. intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return 0;
        $currTime   = CommTime::getTimeStamp(0);
        $isFail     = false;
        $sql        = 'INSERT INTO '.$this->DbAuto->getTableNameAll('theater_conf').' SET '.$setString.', thcf_delete=0, thcf_create_time='.$currTime.', thcf_update_time='.$currTime;
        $thcfId     = $this->DbAuto->getDataId($sql, $isFail);
        if($isFail || $thcfId < 1) return 0;
        return $thcfId;
    }

    /**
     * @param int $thcfId
     * @param array $setData
     * @return bool
     * skitTheaterEdit
     */
    public function skitTheaterEdit(int $thcfId, array $setData):bool
    {
        if($thcfId < 1 || count($setData) < 1) return false;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitTheaterEditDTO::_name:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_name=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterEditDTO::_title:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_title=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterEditDTO::_subTitle:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_sub_title=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitTheaterEditDTO::_skioIdList:
                    $skitInfoList   = SkitInfoCtl::tag()->getSkitInfoSrcBatch($val);
                    $skioIdList     = array_map('intval', array_column($skitInfoList, 'skio_id'));
                    $skioIdList     = array_values(array_intersect($val, $skioIdList));
                    $skioIdJson     = CommJson::encodeArray($skioIdList);
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ids=\''.$this->DbAuto->getRealEscapeString($skioIdJson).'\'';
                    break;
                case SkitTheaterEditDTO::_styleType:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_style_type='.intval($val);
                    break;
                case SkitTheaterEditDTO::_sort:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_sort='.intval($val);
                    break;
                case SkitTheaterEditDTO::_status:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_status='. intval($val);
                    break;
                case SkitTheaterEditDTO::_platform:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'thcf_platform='. intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp(0);
        $sql        = 'UPDATE '.$this->DbAuto->getTableNameAll('theater_conf').' SET '.$setString.', thcf_update_time='.$currTime.' WHERE thcf_id='.$thcfId.' AND thcf_delete=0';
        if($this->DbAuto->getDataNum($sql, $isFail) < 1 || $isFail) return false;
        return true;
    }

    /**
     * @param int $thcfId
     * @return bool
     * skitTheaterDel
     */
    public function skitTheaterDel(int $thcfId):bool
    {
        if($thcfId < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp();
        $resNum     = $this->DbAuto->getDataNum('UPDATE '.$this->DbAuto->getTableNameAll('theater_conf').' SET thcf_delete=1, thcf_update_time='.$currTime.' WHERE thcf_id='.$thcfId.' AND thcf_delete=0', $isFail);
        if($isFail || $resNum < 1) return false;
        return true;
    }
}